home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / winlava.zip / LV.ASM < prev    next >
Assembly Source File  |  1991-09-10  |  5KB  |  238 lines

  1. ; lv.asm - Lava Flow Simulation
  2. ;
  3. ;     (C) Copyright Microsoft Corp. 1991.  All rights reserved.
  4. ;
  5. ;     You have a royalty-free right to use, modify, reproduce and 
  6. ;     distribute the Sample Files (and/or any modified version) in 
  7. ;     any way you find useful, provided that you agree that 
  8. ;     Microsoft has no warranty obligations or liability for any 
  9. ;     Sample Application Files which are modified. 
  10. ;
  11. ?WIN    = 0
  12. ?PLM    = 0
  13. ?386    = 1
  14.         .xlist
  15.         include cmacros.inc
  16.     .list
  17.  
  18.         .386p
  19.  
  20. MAXINT equ 7FFFh
  21. MININT equ 8000h
  22.  
  23.  
  24. ;       The following two equates are just used as shorthand
  25. ;       for the "word ptr" and "byte ptr" overrides.
  26.  
  27. wptr    equ     word ptr
  28. bptr    equ     byte ptr
  29.  
  30. ; The following structure should be used to access high and low
  31. ; words of a DWORD.  This means that "word ptr foo[2]" -> "foo.hi".
  32.  
  33. LONG    struc
  34. lo      dw      ?
  35. hi      dw      ?
  36. LONG    ends
  37.  
  38. FARPOINTER      struc
  39. off     dw      ?
  40. sel     dw      ?
  41. FARPOINTER    ends
  42.  
  43. CHARGE    struc
  44. cgx    dw    ?
  45. cgy    dw    ?
  46. cgvalue dw    ?
  47. CHARGE ends
  48.  
  49. externNP <ulSqrt>
  50.  
  51. sBegin Data
  52. lastvalue dw 0
  53. ;;;;externW nColors
  54. sEnd
  55.  
  56. sBegin Code
  57. assumes cs,code
  58. assumes ds,data
  59. ;;;
  60. ;;;WORD LavaFlowXY(int nCenter, CHARGE aptCenter[], int x, int y)
  61. ;;;{
  62. ;;;    register int    i;
  63. ;;;    DWORD    rgb;
  64. ;;;    static int        n;
  65. ;;;    int    nColors;
  66. ;;;
  67. ;;;    long     dx,dy;
  68. ;;;    long    xSum,ySum;
  69. ;;;    long    d;
  70. ;;;    long     d2;
  71. ;;;    long     adj;
  72. ;;;
  73. ;;;    xSum = ySum = 0;
  74. ;;;    for (i=0; i<nCenter; i++)
  75. ;;;    {
  76. ;;;    dx = aptCenter[i].pt.x - x;
  77. ;;;    dy = aptCenter[i].pt.y - y;
  78. ;;;
  79. ;;;    d2 = dx*dx + dy*dy;
  80. ;;;    // d = sqrt(d2);
  81. ;;;
  82. ;;;    if (d2 == 0)
  83. ;;;       {
  84. ;;;        return n;
  85. ;;;       }
  86. ;;;
  87. ;;;    adj = dx*dy;
  88. ;;;    xSum += (dx*adj*aptCenter[i].value<<4)/(d2);
  89. ;;;    ySum += (dy*adj*aptCenter[i].value<<4)/(d2);
  90. ;;;    }
  91. ;;;
  92. ;;;    xSum >>= 4;
  93. ;;;    ySum >>= 4;
  94. ;;;
  95. ;;;    //d = sqrt(xSum*xSum + ySum*ySum);
  96. ;;;    d = Sqrt(xSum*xSum + ySum*ySum);
  97. ;;;
  98. ;;;    n = d;
  99. ;;;
  100. ;;;    return n;
  101. ;;;}
  102.  
  103. cProc  LavaFlowXY386, PUBLIC
  104.         parmW   nCenter
  105.         parmW   aptCenter
  106.         parmW   x
  107.         parmW   y
  108.         localD  deltax
  109.         localD  deltay
  110.         localD  adj
  111. cBegin
  112.         push    esi
  113.         push    edi
  114.         movzx   eax,ax
  115.         movzx   ebx,bx
  116.         movzx   ecx,cx
  117.         movzx   edx,dx
  118.         movzx   esi,si
  119.         movzx   edi,di
  120.  
  121.         ;;;Loop vars
  122.         ;;;
  123.         ;;; si   pointer to charge array
  124.         ;;; eax  scratch
  125.         ;;; edx  scratch
  126.         ;;; ebx  xSum
  127.         ;;; ecx  ySum
  128.         ;;; edi  d2
  129.  
  130.         ;si points to start of charge array
  131.         mov     si,aptCenter
  132.         ;zero xSum and ySum
  133.         xor     ebx,ebx
  134.         mov     ecx,ebx
  135.  
  136. lava_loop:
  137.         movsx   eax,[si].cgx
  138.         movsx   edx,x
  139.         sub     eax,edx
  140.         mov     deltax,eax
  141.         movsx   edx,[si].cgy
  142.         movsx   edi,y
  143.         sub     edx,edi
  144.         mov     deltay,edx
  145.  
  146.         ;;; eax holds deltax*deltay
  147.         imul    edx
  148.         mov     adj,eax
  149.  
  150.         ;;; get back deltas and calculate d2=deltax*deltax+deltay*deltay
  151.         mov     eax,deltax
  152.         imul    eax
  153.         mov     edi,eax
  154.         mov     eax,deltay
  155.         imul    eax
  156.         add     edi,eax
  157.  
  158.         ; if d2 is zero, we are at a center return last found index
  159.         ;
  160.         mov     ax,lastvalue
  161.         or      edi,edi
  162.         jz      lava_exit
  163.  
  164. ;;; xSum += (dx*adj*aptCenter[i].value<<4)/(d2);
  165. ;;;
  166.         mov     eax,deltax
  167.         imul    adj
  168.  
  169.         ;;; edx:eax holds dx*adj
  170.         movsx   edx,[si].cgvalue
  171.         imul    edx
  172.  
  173.         shl     eax,1
  174.         idiv    edi
  175.         add     ebx,eax
  176.  
  177. ;;; ySum += (dy*adj*aptCenter[i].value<<4)/(d2);
  178. ;;;
  179.         mov     eax,deltay
  180.         imul    adj
  181.         ;;; edx:eax holds dy*adj
  182.         movsx   edx,[si].cgvalue
  183.         imul    edx
  184.  
  185.         shl     eax,1
  186.         idiv    edi
  187.  
  188.         ;;; ySum += eax
  189.         add     ecx,eax
  190.  
  191. lava_next:
  192.         add     si,size CHARGE
  193.         dec     nCenter
  194.         jnz     lava_loop
  195.  
  196. ;;;    xSum >>= 4;
  197. ;;;    ySum >>= 4;
  198. ;;;
  199. ;;;    //d = sqrt(xSum*xSum + ySum*ySum);
  200. ;;;    d = Sqrt(xSum*xSum + ySum*ySum);
  201. ;;;
  202. ;;;    n = d;
  203. ;;;
  204. ;;;    return n;
  205. ;;;
  206.         shr     ebx,1
  207.         shr     ecx,1
  208.  
  209.         mov     eax,ebx
  210.         imul    eax
  211.         mov     edi,eax
  212.         mov     eax,ecx
  213.         imul    eax
  214.         add     eax,edi
  215.  
  216.         push    eax
  217.         cCall   ulSqrt
  218.         add     sp,4
  219.  
  220. ;;;;    dont do MOD here let the caller do it
  221. ;;;;
  222. ;;;;    mov     cx,nColors
  223. ;;;;    xor     dx,dx
  224. ;;;;    div     cx
  225. ;;;;    mov     ax,dx
  226.  
  227.         ;;; save value in static for use at nodes
  228.         mov     lastvalue,ax
  229. lava_exit:
  230.         pop     edi
  231.         pop     esi
  232. cEnd
  233.  
  234.  
  235. sEnd Code
  236.  
  237. end
  238.